//app.js
const QQMapWX = require('./libs/qqmap-wx-jssdk.min.js');
let qqmapsdk;
App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  globalData: {
    userInfo: null
  },
  onLaunchs: function () {
    qqmapsdk = new QQMapWX({
      key: 'FOCBZ-7ZM3I-EMQGT-5CLSU-WICKT-OXBK2' //这里自己的key秘钥进行填充
    })
  },
  globalData: {
    userInfo: null
  },
  post: function (url, status, params) {
    let data = arguments[2] ? arguments[2] : {};
    const p = new Promise((resolve, reject) => {
      // wx.showLoading({
      //   title: 'loading',
      // })
      wx.request({
        url: 'https://api.51huijingang.com/api/merchant/v1' + url,
        method: status,
        data: data,
        header: {
          'content-type': 'application/json', // 默认值
          'token': wx.getStorageSync('token') ? wx.getStorageSync('token') : ''
        },
        success(res) {
          // wx.hideLoading();
          // console.log(res)
          var code = res.statusCode.toString();
          var startChar = code.charAt(0);
          if (startChar == '2') {
            resolve(res.data)
          } else {
            if (code == '404') {
              wx.showModal({
                title: '提示',
                content: res.data.msg,
                showCancel: false
              })

              reject(res.data);
            } else if (code == '500') {
              wx.showModal({
                title: '提示',
                content: res.data.msg,
                showCancel: false
              })
              reject(res.data);
            } else {
              resolve(res)
            }

          }
        },
        // if (res.statusCode) {
        //   wx.showModal({
        //     title: '提示',
        //     content: res.data.msg,
        //     showCancel: false
        //   })
        //   reject(res.data);
        // } else {
        //   resolve(res.data)
        // }
        fail(error) {
          wx.hideLoading();
          console.log(error + "error");
          wx.showModal({
            title: '提示',
            content: error.msg,
            showCancel: false
          })
          reject(error);
        }
      })

    })

    return p;
  },
  getUserLocation() {
    let p = new Promise((resolve, reject) => {
      wx.getLocation({
        type: 'wgs84', success: function (res) {
          // console.log(JSON.stringify(res))        
          var latitude = res.latitude
          var longitude = res.longitude
          let speed = res.speed
          let accuracy = res.accuracy;
          qqmapsdk.reverseGeocoder({
            location: {
              latitude: latitude,
              longitude: longitude
            },
            success: function (res) {
              resolve(res);
            },
            fail: function (res) {
              wx.showModal({
                title: '提示',
                content: '定位失败',
                showCancel: false
              })
            }
          })
        },
        fail: function (res) {
          reject(res);
        }
      })
    })
    return p;
  }
})